<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;

class LoginController extends Controller
{
    public function showLoginForm(){
      $temUsername=Cookie::get('username');
      $temPassword=Cookie::get('password');
      $temrem          =Cookie::get('rem');
        return view('admin.auth.login',compact('temUsername','temPassword','temrem'));
    }

    public function login(Request $request){
        if (Auth::guard('admin')->attempt(['username' => $request->username, 'password' => $request->password,'status'=>1], $request->remember)){
          // if(!Auth::guard('admin')->attempt(['status'=>1]))
          // {
          //   return redirect()->back()->withErrors('用户已被禁用');
          // }
            add_admin_log('用户登录', $request);
            if($request->remember){
              $temUsername=Cookie::make('username',$request->username,1440);
              $temPassword=Cookie::make('password',$request->password,1440);
              $temrem          =Cookie::make('rem','zhen',1440);
              return redirect('/admin')->withCookie($temUsername)->withCookie($temPassword)->withCookie($temrem);
            }else{
              $temUsername=Cookie::make('username','',-1);
              $temPassword=Cookie::make('password','',-1);
              return redirect('/admin')->withCookie($temUsername)->withCookie($temPassword);
            }
        }else{
            return redirect()->back()->withErrors('用户被禁用或用户名密码错误');
        }
    }

    public function logout(){
        Auth::guard('admin')->logout();
        return redirect('/admin/login');
    }
}
